<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label-width="100px" label="轮播图位置" prop="imgName">
                <el-select v-model="queryParams.imgType" placeholder="请选择轮播位置">
                    <el-option v-for="item in bannerList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="学校名称" prop="deptName">
                <el-input v-model="queryParams.deptName" placeholder="请输入学校名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['api:rotateImg:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['api:rotateImg:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['api:rotateImg:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['api:rotateImg:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="rotateImgList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!--  1首页 2机构介绍 3作品展示 4课程体系 -->
            <el-table-column label="轮播位置" align="center" prop="imgName" />
            <el-table-column label="图片预览" align="center" prop="imgUrl">
                <template slot-scope="scope">
                    <img :src="scope.row.imgUrl" alt="" style="max-width: 100px; max-height: 100px;" />
                </template>
            </el-table-column>
            <el-table-column label="年龄" align="center" prop="age" />
            <el-table-column label="指导教师" align="center" prop="teacherName" />
            <!-- 0隐藏   1展示 -->
            <el-table-column label="是否可用" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status" type="success">启用</el-tag>
                    <el-tag v-else type="danger">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="学校名称" align="center" prop="deptName" />
            <el-table-column label="创建人" align="center" prop="createUserId" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['api:rotateImg:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['api:rotateImg:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改轮播图对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="轮播位置" prop="imgName">
                    <el-select v-model="form.imgType" placeholder="请选择轮播位置">
                        <el-option v-for="item in bannerList" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上传图片" prop="imgUrl">
                    <!-- 图片上传 -->
                    <el-upload action="http://39.98.62.132:13001/uploadFile/insert" list-type="picture-card"
                        :multiple="true" :auto-upload="true" :file-list="fileList" :on-change="handleChange"
                        :data="{ userName: 1 }" :on-success="handleSuccess" :before-upload="beforeUpload"
                        >
                        <i slot="default" class="el-icon-plus"></i>
                        <div slot="file" slot-scope="{file}">
                            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                            <span class="el-upload-list__item-actions">
                                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                                    <i class="el-icon-zoom-in"></i>
                                </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete"
                                    @click="handleDownload(file)">
                                    <i class="el-icon-download"></i>
                                </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                                    <i class="el-icon-delete"></i>
                                </span>
                            </span>
                        </div>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="是否可用" prop="status">
                    <el-radio v-model="form.status" label="1">启用</el-radio>
                    <el-radio v-model="form.status" label="0">禁用</el-radio>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入年龄" />
                </el-form-item>
                <el-form-item label="指导教师" prop="teacherName">
                    <el-input v-model="form.teacherName" placeholder="请输入指导教师" />
                </el-form-item>
                <el-form-item label="学校名称" prop="deptName">
                    <el-input v-model="form.deptName" placeholder="请输入学校名称" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listRotateImg, getRotateImg, delRotateImg, addRotateImg, updateRotateImg } from "@/api/banner/banner";

export default {
    name: "RotateImg",
    data() {
        return {
            //图片上传
            fileList: [], // 当前选择的文件列表
            dialogImageUrl: '',
            dialogVisible: false,
            disabled: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 轮播图表格数据
            rotateImgList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                imgUrl: null,
                imgType: null,
                link: null,
                imgName: null,
                age: null,
                teacherName: null,
                status: null,
                deptId: null,
                createUserId: null,
                updateUserId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {

            },
            bannerList: [
                {
                    value: '',
                    label: '全部'
                }, {
                    value: '1',
                    label: '首页轮播图'
                }, {
                    value: '2',
                    label: '机构介绍'
                }, {
                    value: '3',
                    label: '作品展示'
                }, {
                    value: '4',
                    label: '课程体系'
                }],
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /* 图片上传 */
        handleChange(file, fileList) {
            // 保留最新上传的一个文件
            this.fileList = fileList.slice(-1);
        },
        beforeUpload(file) {
            if (this.fileList.length > 1) {
                this.$message.error('只能上传一个文件');
                return false; // 阻止上传
            }
            return true;
        },
        handleRemove(file) {
            // 移除文件时更新文件列表
            this.fileList = [];
            this.$refs.upload.clearFiles(); // 清除上传文件
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        handleDownload(file) {
            console.log(file);
        },
        handleSuccess(response) {
            // 处理上传成功的响应
            if (response.code == 200) {
                console.log('上传成功:', response);
                this.form.imgUrl = response.data;
            } else if (response.code == 500) {
                this.$message.error('上传失败:'+ response.msg);
                this.fileList = [];
            }
        },
        /** 查询轮播图列表 */
        getList() {
            this.loading = true;
            listRotateImg(this.queryParams).then(response => {
                this.rotateImgList = response.rows;
                console.log();
                this.rotateImgList.forEach(item => {
                    if (item.imgType == "1") {
                        item.imgName = '首页轮播图'
                    }
                    if (item.imgType == "2") {
                        item.imgName = '机构介绍'
                    }
                    if (item.imgType == "3") {
                        item.imgName = '作品展示'
                    }
                    if (item.imgType == "4") {
                        item.imgName = '课程体系'
                    }
                })

                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.fileList = [];
            this.form = {
                rotateImgId: null,
                imgUrl: null,
                imgType: null,
                link: null,
                imgName: null,
                age: null,
                teacherName: null,
                status: null,
                deptId: null,
                createTime: null,
                updateTime: null,
                createUserId: null,
                updateUserId: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.rotateImgId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加轮播图";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const rotateImgId = row.rotateImgId || this.ids
            getRotateImg(rotateImgId).then(response => {
                this.form = response.data;
                this.form.status = response.data.status + ""; // 转换为字符串
                if (response.data.imgUrl) {
                    this.fileList.push({ url: response.data.imgUrl, status: 'success' });
                }
                this.open = true;
                this.title = "修改轮播图";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.rotateImgId != null) {
                        updateRotateImg(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addRotateImg(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const rotateImgIds = row.rotateImgId || this.ids;
            this.$modal.confirm('是否确认删除轮播图编号为"' + rotateImgIds + '"的数据项？').then(function () {
                return delRotateImg(rotateImgIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('api/rotateImg/export', {
                ...this.queryParams
            }, `rotateImg_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
